package servlet.task;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import beans.Customer;
import beans.Info;
import beans.Task;
import service.CustomerService;
import service.TaskService;

@WebServlet("/TaskOperationServlet")
public class TaskOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TaskOperationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TaskService service = new TaskService();

		String operation = request.getParameter("operation");
		String id = request.getParameter("id");
		String page = request.getParameter("page");
		int customerId = ((Customer)request.getSession().getAttribute("customer")).getId();
		switch (operation) {
		case "getAll":
			response.getWriter().write(new Gson().toJson(service.getTask(page).list));
			break;
		case "orderedTask":
			response.getWriter().write(service.orderedTask(customerId, page));
			break;
		case "orderTask":
			String orderTaskMessage = "抱歉，订单已经被接了！";
			Task orderTask = service.getTaskById(id).list.get(0);
			if (orderTask.getOrderCustomer() == null) {
				service.orderTask(id, customerId);
				orderTaskMessage = "接单成功！请尽快完成！";
			}
			response.getWriter().write(orderTaskMessage);
			break;
		case "getById" :
			response.getWriter().write(new Gson().toJson(service.getTaskById(id)));
			break;
		case "del":
			service.delTask(id);
			break;
		case "alt": 
			// 修改任务信息
			if (id != null) {
				Info<Task> task = service.getTaskById(id);
				request.setAttribute("task", task.list.get(0));
				request.getRequestDispatcher("/admin/taskinfo.jsp").forward(request, response);
				return;
			}

			String tid = request.getParameter("tid");
			String name = request.getParameter("name");
			String tel = request.getParameter("tel");
			String message = request.getParameter("message");
			String money = request.getParameter("money");
			String address = request.getParameter("address");
			service.alterTask(tid, name, tel, message, money, address);
			response.sendRedirect("TaskServlet?id=" + tid);
			break;
		case "add":
			// 发布任务信息
			String content = request.getParameter("content");
			String otherContent = request.getParameter("otherContent");
			tel = request.getParameter("tel");
			address = request.getParameter("address");
			money = request.getParameter("money");
			content += "<br><br>" + otherContent;
			Customer customer = (Customer) request.getSession().getAttribute("customer");
			if (!customer.getTel().equals(tel)) {
				customer.setTel(tel);
				new CustomerService().alterCustomer(String.valueOf(customer.getId()), customer.getUsername(),
						customer.getSex(), String.valueOf(customer.getMoney()), customer.getTel(), customer.getAddr(),
						customer.getSchool(), customer.getWechat());
			}
			if (!customer.getAddr().equals(address)) {
				customer.setAddr(address);
				new CustomerService().alterCustomer(String.valueOf(customer.getId()), customer.getUsername(),
						customer.getSex(), String.valueOf(customer.getMoney()), customer.getTel(), customer.getAddr(),
						customer.getSchool(), customer.getWechat());
			}
			Task tempTask = new Task(0, customer, content, Double.parseDouble(money), address, "未接单");
			service.addTask(tempTask);
			response.sendRedirect(request.getContextPath() + "/index.html");
			break;
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
